<?php
namespace app\common\model\mysql;

use think\Model;

class Category extends Model {
    // 自动生成写入时间
    protected $autoWriteTimestamp = true;

    public function getNormalCategorys($field = '*') {
        $where = [
            'status'=>config('status.mysql.table_normal')
        ];
        $order = [
            'listorder'=>'desc',
            'id'=>'desc'
        ];
        return $this->where($where)->field($field)->order($order)->select();
    }

    public function getLists($where, $num = 10) {
        $order = [
            'listorder'=>'desc',
            'id'=>'desc'
        ];
        return $this->where('status','<>', config('status.mysql.table_delete'))
            ->where($where)
            ->order($order)
            ->paginate($num);
    }

    public function getChildCountInPids($condition) {
        $where[] = ['pid', 'in', $condition['pid']];
        $where[] = ['status', '<>', config('status.mysql.table_delete')];
        $res = $this->where($where)->field(['pid', 'count(1) as count'])
            ->group('pid')
            ->select();
        return $res;
    }

    /**
     * 根据id 更新数据库里面的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id, $data){
        $data['update_time'] = time();
        return $this->where(['id'=>$id])->save($data);
    }

    public function getNormalByPid($pid = 0, $field) {
        $where = [
            'pid'=>$pid,
            'status'=>config('status.mysql.table_normal')
        ];
        $order = [
            'listorder'=>'desc',
            'id'=>'desc'
        ];
        return $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
    }
}